/**
 * 功能：使用该jQuery插件来制作在线签名或涂鸦板，用户绘制的东西可以用图片的形式保存下来。
 * 作者：黄金锋 (549387177@qq.com)
 * 日期：2015-11-16  13:51:01
 * 版本：version 1.0
 */
var WritingPad = function () {


    function initHtml() {

        var html = '<div class="modal inmodal" aria-hidden="true" tabindex="1"  id="canvasmodal">' +
            '<div class="modal-dialog">' +
            '<div class="modal-content">' +
            '<div class="modal-header">' +
            '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>' +
            '<h4 class="modal-title">手写面板</h4>' +
            '</div>' +
            '<div class="modal-body">' +
            '<div class="js-signature" id="mySignature">' +
            '</div>' +
            '<div>' +
            '<button type="button" class="btn btn-default" id="myEmpty">清空面板</button>' +
            '<div id="colorpanel" style="position:absolute;z-index:99;display:none"></div>' +
                //'</div>'+
            '</div>' +
            '</div>' +
            '<div class="modal-footer"><canvas id="canvas" style="display: none"></canvas>' +

            '<button type="button" class="btn btn-default" id="myClose">关闭</button>' +
            '<button type="button" class="btn btn-primary" id="mySave">保存</button>' +
            '<div id="myImg">' +
            '<div>' +

            '</div>' +
            '</div>' +
            '</div>' +
            '</div>';

        $('body').append(html);

    }

    function initTable() {
        var colorTable = "";
        var ColorHex = new Array('00', '33', '66', '99', 'CC', 'FF');
        var SpColorHex = new Array('FF0000', '00FF00', '0000FF', 'FFFF00', '00FFFF', 'FF00FF');
        for (var i = 0; i < 2; i++)
        {
            for (var j = 0; j < 6; j++)
            {
                colorTable = colorTable + '<tr height=12>';
                colorTable = colorTable + '<td width=11 style="background-color:#000000"></td>';

                if (i == 0)
                {
                    colorTable = colorTable + '<td width=11 style="background-color:#' + ColorHex[j] + ColorHex[j] + ColorHex[j] + '"></td>';
                }
                else
                {
                    colorTable = colorTable + '<td width=11 style="background-color:#' + SpColorHex[j] + '"></td>';
                }

                //colorTable = colorTable + '<td width=11 style="background-color:#000000"></td>';

                for (var k = 0; k < 3; k++)
                {
                    for (l = 0; l < 6; l++)
                    {
                        colorTable = colorTable + '<td width=11 style="background-color:#' + ColorHex[k + i * 3] + ColorHex[l] + ColorHex[j] + '"></td>';
                    }
                }
                colorTable = colorTable + '</tr>';


            }
        }
        colorTable =
            '<table border="1" id="myTable" cellspacing="0" cellpadding="0" style="border-collapse: collapse;cursor:pointer;" bordercolor="000000">'
            + colorTable + '</table>' +
            '<table width=225 border="0" cellspacing="0" cellpadding="0" style="border:1px #000000 solid;border-collapse: collapse;background-color:#000000">' +
            '<tr style="height:30px">' +
            '<td colspan=21 bgcolor=#cccccc>' +

            '<table cellpadding="0" cellspacing="1" border="0" style="border-collapse: collapse">' +
            '<tr>' +
            '<td width="3"><input type="text" name="DisColor" size="6" disabled style="border:solid 1px #000000;background-color:#ffff00"></td>' +
            '<td width="3"><input type="text" name="HexColor" size="7" style="border:inset 1px;font-family:Arial;" value="#000000"></td>' +
            '<td width="3"><button type="button" class="btn btn-primary btn-sm" id="btnSave">确认</button></td>' +
            '</tr>' +
            '</table>' +

            '</td>' +
            '</tr>' +
            '</table>';
        $("#colorpanel").append(colorTable);
    }

    function initSignature() {

        if (window.requestAnimFrame) {
            var signature = $("#mySignature");
            signature.jqSignature({ width: 540, height: 300, border: 'none', background: '#ffffff', lineColor: '#000000', lineWidth: 1, autoFit: false });
            //{ width: 600, height: 200, border: '1px solid red', background: '#16A085', lineColor: '#ABCDEF', lineWidth: 2, autoFit: true }
        } else {

            alert("请加载jq-signature.js");
            return;
        }
    }

    function showRGB(arr) {
        hexcode = "#";
        for (x = 0; x < 3; x++) {
            var n = arr[x];
            if (n == "") n = "0";
            if (parseInt(n) != n)
                return alert("RGB颜色值不是数字！");
            if (n > 255)
                return alert("RGB颜色数字必须在0-255之间！");
            var c = "0123456789ABCDEF", b = "", a = n % 16;
            b = c.substr(a, 1); a = (n - a) / 16;
            hexcode += c.substr(a, 1) + b
        }
        return hexcode;
    }

    function init(jqueryDom,base) {
        console.log(jqueryDom);
        if ($("#canvasmodal")) {
            $("#canvasmodal").modal("toggle");
        } else {
            alert("没用手写面板");
        }

        $('.modal').on("click", "#myClose,.close", null, function () {
            $('#canvasmodal').modal('hide');
            $("#canvasmodal").remove();
            $('.modal-scrollable').remove();
            $('.modal-backdrop').remove();
        });

        $('.modal').on("click", "#mySave", function () {
            console.log(jqueryDom);
            var myImg = $('#myImg').empty();
            var dataUrl = $('.js-signature').jqSignature('getDataURL');
            if(typeof dataUrl!="object"){
                console.log(dataUrl);
                var img= new Image();
                img.src= dataUrl;
                img.onload= function (ev) {
                    var canvas= document.getElementById("canvas");

                    canvas.width= img.width;
                    canvas.height= img.height;
                    var ctx= canvas.getContext("2d");
                    ctx.drawImage(img,0,0);
                    var imagedate= ctx.getImageData(0,0,img.width,img.height);
                    var y=0;
                    for (var i=imagedate.height; i>0; i--){
                        for (var j=imagedate.width; j>0;j--){
                            var x= i*4*imagedate.height+4*j,
                                r= imagedate.data[x],
                                a= imagedate.data[x+3];
                            if (y===0 && a!=0){
                                y=i;
                            }
                        }
                    }
                    console.log(y);
                    ctx.clearRect(0,0,img.width,img.height);
                    canvas.height=y;
                    ctx.drawImage(img,0,0);
                    var data=canvas.toDataURL();
                    uploadHandler(data,jqueryDom,base);
                }

                /*

                 */
            }
        });

        $(document).on("click", "#myEmpty", null, function () {
            $('.js-signature').jqSignature('clearCanvas');

        });

        $(document).on("click", "#myBackColor", null, function () {

            $('#colorpanel').css('left', '95px').css('top', '45px').css("display", "block").fadeIn();
            //$("canvas").css("background", "#EEEEEE");
            $("#btnSave").data("sender", "#myBackColor");
        });

        $(document).on("click", "#myColor", null, function () {
            $('#colorpanel').css('left', '205px').css('top', '45px').css("display", "block").fadeIn();
            $("#btnSave").data("sender", "#myColor");
        });

        $(document).on("mouseover", "#myTable", null, function () {

            if ((event.srcElement.tagName == "TD") && (current != event.srcElement)) {
                if (current != null) { current.style.backgroundColor = current._background }
                event.srcElement._background = event.srcElement.style.backgroundColor;
                //$("input[name=DisColor]").css("background-color", event.srcElement.style.backgroundColor);
                //var color = event.srcElement.style.backgroundColor;
                //var strArr = color.substring(4, color.length - 1).split(',');
                //var num = showRGB(strArr);
                //$("input[name=HexColor]").val(num);
                current = event.srcElement;
            }

        });

        $(document).on("mouseout", "#myTable", null, function () {

            if (current != null) current.style.backgroundColor = current._background

        });

        $(document).on("click", "#myTable", null, function () {

            if (event.srcElement.tagName == "TD") {
                var color = event.srcElement._background;
                if (color) {
                    $("input[name=DisColor]").css("background-color", color);
                    var strArr = color.substring(4, color.length - 1).split(',');
                    var num = showRGB(strArr);
                    $("input[name=HexColor]").val(num);
                }
            }

        });

        $(document).on("click", "#btnSave", null, function () {

            $('#colorpanel').css("display", "none");
            var typeData = $("#btnSave").data("sender");
            var HexColor = $("input[name=HexColor]").val();
            var data = $(".js-signature").data();
            if (typeData == "#myColor") {
                data["plugin_jqSignature"]["settings"]["lineColor"] = HexColor;
                $('.js-signature').jqSignature('reLoadData');
            }
            if (typeData == "#myBackColor") {

                data["plugin_jqSignature"]["settings"]["background"] = HexColor;
                $('.js-signature').jqSignature('reLoadData');
            }
        });

        $("#canvasmodal").on('hide.bs.modal', function () {
            $("#colorpanel").remove();
            $("#canvasmodal").remove();
            $("#myTable").remove();
        });

    }

    var uploadHandler= function(data,jqueryDom,base){
        var param=new Object();
        param.base64=data;
        param.type=8;
        var url=base+"/common/uploadBase64";
        $("#canvasmodal").modal('hide');
        $('.modal-scrollable').remove();
        $('.modal-backdrop').remove();
        $.ajax({
            type:"POST",
            url:url,
            data:param,
            dataType:"json",
            success:function(data){
                if (data.success){
                    if (!jqueryDom.hasClass('btn')){
                        var src= jqueryDom.attr('src');
                        jqueryDom.attr("src",data.data);
                        $('input[type=hidden]').each(function(){
                            if ($(this).val()==src){
                                $(this).val(data.data);
                            }
                        });
                        return;
                    }
                    var img = $('<img>').attr('src',data.data);
                    var name= jqueryDom.attr('name');
                    name=name+"_value";
                    var input='<input type="hidden" name="'+name+'" value="'+data.data+'">';
                    jqueryDom.before(img);
                    jqueryDom.before(input);
                    jqueryDom.before("<br/>");
                }
            }
        })
    };

    return {
        init: function (dom,base) {
            init();

            initHtml();

            initTable();

            initSignature();

            init(dom,base);

            console.log(dom);
        }
    };
}();